package com.antler.smt.portal.service.impl;

import com.antler.smt.constant.DateTimeConstant;
import com.antler.smt.entity.AttachmentEntity;
import com.antler.smt.entity.MemberEntity;
import com.antler.smt.entity.MessagePushEntity;
import com.antler.smt.entity.MessageReadHistoryEntity;
import com.antler.smt.enums.ResultCodeEnum;
import com.antler.smt.portal.mapper.*;
import com.antler.smt.response.base.Result;
import com.antler.smt.service.portal.MessageReadHistoryService;
import com.antler.smt.utils.DateUtil;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author zhangyi
 * @date 2018/5/17 18:21
 */
@Service
public class MessageReadHistoryServiceImpl implements MessageReadHistoryService {


    @Autowired
    private MessageReadHistoryInfoMapper messageIsReadInfoMapper;

    @Autowired
    private MessagePushInfoMapper messagePushInfoMapper;

    @Autowired
    private MemberEntityMapper memberEntityMapper;

    @Autowired
    private MessageReadHistoryEntityMapper messageReadHistoryEntityMapper;

    @Autowired
    private AttachmentEntityMapper attachmentEntityMapper;

    /**
     * 是否阅读
     *
     * @param id 阅读推送关联表id
     * @return
     */
    @Override
    public Result isRead(Long id,Long memberId) {
        Result result = null;
        MessageReadHistoryEntity message = new MessageReadHistoryEntity();
        message.setMessagePushId(id);
        message.setMemberId(memberId);
        message.setCreateDate(DateUtil.stampToDate(System.currentTimeMillis()));
        message.setUpateDate(DateUtil.stampToDate(System.currentTimeMillis()));
        int i = messageReadHistoryEntityMapper.insertSelective(message);
        result = (0 == i?new Result(ResultCodeEnum.APPEND_DATA_ERROR):new Result(ResultCodeEnum.OPERATION_SUCCESS));
        return result;
    }

    /**
     * 分类获取是否阅读的通知
     *
     * @param memberId
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = DateTimeConstant.MINUTE_S, rollbackFor = Exception.class)
    public Result getClassifyMessage(Long memberId) {
        Result result = null;

        List<Object> list = new ArrayList<>();

        MemberEntity memberEntity = memberEntityMapper.selectByPrimaryKey(memberId);
        Page<MessagePushEntity> allMessagePush = messagePushInfoMapper.getAllMessagePush(memberEntity.getType());

        for(int i = 0; i < allMessagePush.size();i++){
            AttachmentEntity attachmentEntity = attachmentEntityMapper.selectByPrimaryKey(allMessagePush.get(i).getPushImagesId());
            if(attachmentEntity != null) {
                allMessagePush.get(i).setImgName(attachmentEntity.getFileName());
            }
        }
        List<Integer> currentIsRead1 = messageIsReadInfoMapper.getCurrentIsRead(memberId);
        List<MessagePushEntity> listMessage = new ArrayList<>();
            for(int j = 0; j < currentIsRead1.size(); j++){
                for(int i = 0; i < allMessagePush.size(); i++){
                    if(String.valueOf(allMessagePush.get(i).getId()).equals(String.valueOf(currentIsRead1.get(j)))){
                        listMessage.add(allMessagePush.get(i));
                        allMessagePush.remove(i);
                        break;
                    }
                }
            }
        list.add(listMessage);
        list.add(allMessagePush);
        result = new Result(list);
        return result;
    }
}
